﻿using BCCommon;
using BCData.TMS.Order.OrderAssignRecord;
using BCDto.Sim.Admin.Admin;
using BCDto.TMS.Order.OrderAssignRecord;
using BCEntity.Common.EntityNameConst;
using BCEntity.TMS.Order.OrderAssignRecord;
using BCService.Utils;
using System;
using System.Collections.Generic;

namespace BCService.TMS.Order.OrderAssignRecord
{
    public class OrderAssignRecordService : IOrderAssignRecordService
    {
        #region 私有变量
        private IOrderAssignRecordData orderAssignRecordData;
        private readonly IRedisService redisService;
        private readonly string key = string.Format("{0}", EntityNameConst.OrderAssignRecordEntity);
        #endregion

        #region 构造函数
        public OrderAssignRecordService(IOrderAssignRecordData orderAssignRecordData, IRedisService redisService)
        {
            this.orderAssignRecordData = orderAssignRecordData;
            this.redisService = redisService;
        }
        #endregion

        #region 修改订单指派记录
        /// <summary>
        /// 修改订单指派记录
        /// </summary>
        /// <param name="orderAssignRecordDto">订单指派纪录</param>
        /// <returns></returns>
        public OrderAssignRecordDto Update(OrderAssignRecordDto orderAssignRecordDto)
        {
            var result = orderAssignRecordData.UpdateAsync(orderAssignRecordDto.As<OrderAssignRecordEntity>()).GetAwaiter().GetResult();
            if (result == null)
            {
                throw new ArithmeticException("修改订单指派记录失败");
            }
            string cacheKey = this.redisService.GetKey(key, orderAssignRecordDto.OrderAssignRecordId);
            this.redisService.DeleteMultipleKey(cacheKey, false);
            return result.As<OrderAssignRecordDto>();

        }
        #endregion

        #region 删除订单指派记录
        /// <summary>
        /// 删除订单指派记录
        /// </summary>
        /// <param name="orderAssignRecordId">订单指派纪录Id</param>
        /// <returns></returns>
        public int Delete(long orderAssignRecordId)
        {
            var result = orderAssignRecordData.DeleteAsync(orderAssignRecordId).GetAwaiter().GetResult();
            if (result <= 0)
            {
                throw new ArithmeticException("删除订单指派记录失败");
            }
            string cacheKey = this.redisService.GetKey(key, orderAssignRecordId);
            this.redisService.DeleteMultipleKey(cacheKey, false);
            return result;
        }
        #endregion

        #region 获取订单指派记录
        /// <summary>
        /// 获取订单指派记录
        /// </summary>
        /// <param name="orderAssignRecordId">订单指派纪录Id</param>
        /// <returns></returns>
        public OrderAssignRecordDto GetByOrderAssignRecordId(long orderAssignRecordId)
        {
            string cacheKey = this.redisService.GetKey(key, orderAssignRecordId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = orderAssignRecordData.GetByOrderAssignRecordIdAsync(orderAssignRecordId).GetAwaiter().GetResult();
                return data.As<OrderAssignRecordDto>();
            }, TimeSpan.FromDays(1));
            return result;
        }
        #endregion

        #region 分页获取订单指派记录
        public PageableList<OrderAssignRecordDto> GetList(AdminDto adminDto, long? orderId, int pageIndex, int pageSize)
        {
            var (list, count) = orderAssignRecordData.GetListAsync(null, null, null, orderId, pageIndex, pageSize).GetAwaiter().GetResult();
            return new PageableList<OrderAssignRecordDto>()
            {
                Count = count,
                Items = list.As<IEnumerable<OrderAssignRecordDto>>(),
                PageIndex = pageIndex,
                PageSize = pageSize,
                ServerTime = DateTime.Now,
            };
        }
        #endregion
    }
}
